#$ ftpstat (ftpstatistics) version 3(b) by jason stiefel <krypto@zianet.com>
#$ copyright transmit liberation productions[tm] -(c)1996- liberation.org
#$ usage- (run the ftpstat script, i.e. #~ sh ftpstat)

config () # feel free to change anything in this function
{
	 ftpstat_version="3(beta)"
	 ftpstat_tagline="ftpstat version ${ftpstat_version} ftp://ftp.liberation.org - krypto@zianet.com"
	 in_welcome_file="/home/ftp/welcome.stat" # unprocessed welcome
	out_welcome_file="/home/ftp/welcome.msg"  # proccessed welcome
	     last_update="`date`"                   # current date/time
		   adder="/usr/bin/adder"	  # location of adder
		 xferlog="/var/adm/xferlog"       # location of xferlog
	      messagelog="/var/adm/messages"      # location of messages
	         tmp_one="/tmp/.ftpstat.tmp"      # trash file one
		 tmp_two="/tmp/.ftpstat.sizes"    # trash file two
		 tmp_tre="/tmp/.ftpstat.tmp3"     # trash file tre
		 tmp_qua="/tmp/.ftpstat.tmp4"     # trash file qua
		 tmp_sin="/tmp/.ftpstat.tmp5"     # trash file sin
	       data_file="/tmp/.ftpstat.data"     # data file for ftpstat
	     logins_char="%%l"       # character to replace logins with
	      files_char="%%f"       # character to replace files with
	       size_char="%%s"       # character to replace size with
		avg_char="%%a"       # character to replace average with
	       last_char="%%t"       # character to replace last update

	if [ -e ${data_file} ]; then
		old_logins=`sed 1q ${data_file}`
		old_files=`sed 2q ${data_file} | tail -n 1`
		old_size=`sed 3q ${data_file} | tail -n 1`
		old_avg=`sed 4q ${data_file} | tail -n 1`
	else
		old_logins="*first run*"
		old_files="*first run*"
		old_size="*first run*"
		old_avg="*first run*"
	fi
}

login_func ()
{ 
	logins=`cat ${messagelog} | grep 'wu.ftpd' | grep 'connect' | cat -n | tail -n 1 | awk '{print $1}'` 
}				

files_func ()
{ 
	files=`cat -n ${xferlog} | tail -n 1 | awk '{print $1}'` 
} 	

sizes_func ()
{
	if [ -e ${tmp_two} ]; then
		rm ${tmp_two} 
	fi
	cat ${xferlog} | awk '{print $8}' >> ${tmp_two}		
	echo "0" >> ${tmp_two}
	total_size=`cat ${tmp_two} | ${adder} | tail -n 1`
	average=`expr ${total_size} / ${files}` 
}

output_data ()
{
        # echo "last update= ${last_update}"
	cat ${in_welcome_file} | sed /${logins_char}/s//${logins}/ > ${tmp_one}
	cat ${tmp_one} | sed /${files_char}/s//${files}/ > ${tmp_tre}
	cat ${tmp_tre} | sed /${size_char}/s//${total_size}/ > ${tmp_qua}
	cat ${tmp_qua} | sed /${avg_char}/s//${average}/ > ${out_welcome_file}
	echo ${ftpstat_tagline} >> ${out_welcome_file}
	echo ${logins} > ${data_file} ; echo ${files} >> ${data_file} ; echo ${total_size} >> ${data_file} ; echo ${average} >> ${data_file}
	if [ ${1} = "-v" ]; then
		echo "`uname -n` ftp statistics:"
		echo
		echo "          logins-"
		echo "                  old/new: ${old_logins}/${logins}"
		echo "          files -"
		echo "                  old/new: ${old_files}/${files}"
		echo " transfer total -"
		echo "                  old/new: ${old_size}/${total_size}"	
		echo "   average size -"
		echo "                  old/new: ${old_avg}/${average}"
		echo
		echo "ftpstat version ${ftpstat_version}" 
	fi	
}

# this is the main code of the script....

config
login_func
files_func
sizes_func
if [ -z ${1} ]; then
	output_data o
	else
	output_data -v
fi
